import 'package:flutter/material.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/seckill_order_btns.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_order_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_list_vo.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SeckillOrderGoodArr extends StatefulWidget {

  final BlindBoxOrderListVo seckillOrderListVo;
  final SeckillOrderPageController seckillOrderPageController;

  SeckillOrderGoodArr({
    required this.seckillOrderListVo,
    required this.seckillOrderPageController
  });
  @override
  _SeckillOrderGoodArrState createState() => _SeckillOrderGoodArrState();
}

class _SeckillOrderGoodArrState extends State<SeckillOrderGoodArr> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (widget.seckillOrderListVo.data!.length <= 0) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/icons/icon_blindbox_order_list_def.png',
              width: 273.w,
              height: 146.h,
            ),
            SizedBox(height: 10.h),
            Text(
              '还没订单~去下一单！',
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            )
          ],
        ),
      );
    }
    return RefreshUtil.easyRefresh(
      onRefresh: () async {
        widget.seckillOrderPageController.getSeckillTabItemOrderList(widget.seckillOrderPageController.tabIndex.value);
      },
      onLoad: () async {
        widget.seckillOrderPageController.onloadSeckillOrderList();
      },
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            BlindBoxOrderItem item = widget.seckillOrderListVo.data![index];
            return Container(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              margin: EdgeInsets.only(bottom: 12.h),
              child: InkWellUtil.shakeProofInkWell(Container(
                decoration: BoxDecoration(
                  color: bgWhiteColor,
                  borderRadius: BorderRadius.circular(5.w)
                ),
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 8.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: .5,
                              color: lineColor
                            )
                          )
                        ),
                        child: Row(
                          children: [
                            Expanded(child: Text(
                              '订单号：${item.orderSn}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: TextStyle(
                                color: textBlackColor,
                                fontWeight: FontWeight.bold,
                                fontSize: 14.sp
                              ),
                            )),
                            Text(
                              '${item.statusStr}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 14.sp
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: .5,
                              color: lineColor
                            )
                          )
                        ),
                        child: Column(
                          children: [
                            Column(
                              children: item.packages!.map((productItem) {
                                return Container(
                                  margin: EdgeInsets.only(bottom: 12.w),
                                  child: Row(
                                    children: [
                                      NetworkImageUtil(
                                        url: productItem.image ?? '', 
                                        width: 76.w, 
                                        height: 76.w,
                                        borderRadius: 5.w,
                                      ),
                                      SizedBox(width: 11.w),
                                      Expanded(child: Container(
                                        constraints: BoxConstraints(
                                          minHeight: 76.w
                                        ),
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '${productItem.title}',
                                              overflow: TextOverflow.ellipsis,
                                              maxLines: 2,
                                              style: TextStyle(
                                                fontSize: 14.sp,
                                                color: textBlackColor
                                              ),
                                            ),
                                            SizedBox(height: 12.w),
                                            Text(
                                              '${item.createdAt}',
                                              style: TextStyle(
                                                color: textSubtitleColor,
                                                fontSize: 12.sp
                                              ),
                                            )
                                          ],
                                        ),
                                      ))
                                    ],
                                  ),
                                );
                              }).toList(),
                            ),
                            Container(
                              alignment: Alignment.centerRight,
                              child: Text.rich(
                                TextSpan(
                                  text: '合计支付：',
                                  style: TextStyle(
                                    color: textBlackColor,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.bold
                                  ),
                                  children: [
                                    TextSpan(
                                      text: '¥',
                                      style: TextStyle(
                                        color: textBlackColor,
                                        fontSize: 12.sp,
                                        fontWeight: FontWeight.bold
                                      )
                                    ),
                                    TextSpan(
                                      text: '${item.totalAmount}',
                                      style: TextStyle(
                                        color: textBlackColor,
                                        fontSize: 16.sp,
                                        fontWeight: FontWeight.bold
                                      )
                                    )
                                  ]
                                )
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        alignment: Alignment.centerRight,
                        child: item.status == 1 ? SeckillOrderBtns(
                          status: item.status!, 
                          toPayFun: () {
                            widget.seckillOrderPageController.goPay(item.payableAmount!, item.orderSn!, item.id!, item.status!);
                          }, 
                          cleanOrderFun: () {
                            widget.seckillOrderPageController.closeSeckillOrder(item.id!);
                          },
                          confirmReceiptFun: () {}
                        ) : Container(
                          width: 90.w,
                          margin: EdgeInsets.only(left: 12.w),
                          child: ButtonUtil(
                            text: '查看订单', 
                            fontSize: 14.w, 
                            borderRadius: 18.w, 
                            height: 36.h, 
                            onTop: () {
                              Get.toNamed(
                                '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAGE}${AppRouters.SECKILL_ORDER_DETAIL_PAGE}',
                                arguments: {
                                  'seckillOrderId': item.id!,
                                  'isSeckillPage': false,
                                  'title': item.statusStr
                                }
                              );
                            }
                          ),
                        )
                      )
                    ],
                  ),
                ),
              ), () {
                Get.toNamed(
                  '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAGE}${AppRouters.SECKILL_ORDER_DETAIL_PAGE}',
                  arguments: {
                    'seckillOrderId': item.id!,
                    'isSeckillPage': false,
                    'title': item.statusStr
                  }
                );
              }),
            );
          }, childCount: widget.seckillOrderListVo.data!.length),
        ),
      ]
      // slivers: widget.seckillOrderListVo.data!.map((item) {
      //   return SliverToBoxAdapter(
      //     child: Container(
      //       padding: EdgeInsets.symmetric(horizontal: 16.w),
      //       margin: EdgeInsets.only(bottom: 12.h),
      //       child: InkWellUtil.shakeProofInkWell(Container(
      //         decoration: BoxDecoration(
      //           color: bgWhiteColor,
      //           borderRadius: BorderRadius.circular(5.w)
      //         ),
      //         child: Container(
      //           padding: EdgeInsets.symmetric(horizontal: 8.w),
      //           child: Column(
      //             crossAxisAlignment: CrossAxisAlignment.start,
      //             children: [
      //               Container(
      //                 padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
      //                 decoration: BoxDecoration(
      //                   border: Border(
      //                     bottom: BorderSide(
      //                       width: .5,
      //                       color: lineColor
      //                     )
      //                   )
      //                 ),
      //                 child: Row(
      //                   children: [
      //                     Expanded(child: Text(
      //                       '订单号：${item.orderSn}',
      //                       overflow: TextOverflow.ellipsis,
      //                       maxLines: 1,
      //                       style: TextStyle(
      //                         color: textBlackColor,
      //                         fontWeight: FontWeight.bold,
      //                         fontSize: 14.sp
      //                       ),
      //                     )),
      //                     Text(
      //                       '${item.statusStr}',
      //                       style: TextStyle(
      //                         color: mainTextColor,
      //                         fontSize: 14.sp
      //                       ),
      //                     )
      //                   ],
      //                 ),
      //               ),
      //               Container(
      //                 padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
      //                 decoration: BoxDecoration(
      //                   border: Border(
      //                     bottom: BorderSide(
      //                       width: .5,
      //                       color: lineColor
      //                     )
      //                   )
      //                 ),
      //                 child: Column(
      //                   children: [
      //                     Column(
      //                       children: item.packages!.map((productItem) {
      //                         return Container(
      //                           margin: EdgeInsets.only(bottom: 12.w),
      //                           child: Row(
      //                             children: [
      //                               NetworkImageUtil(
      //                                 url: productItem.image ?? '', 
      //                                 width: 76.w, 
      //                                 height: 76.w,
      //                                 borderRadius: 5.w,
      //                               ),
      //                               SizedBox(width: 11.w),
      //                               Expanded(child: Container(
      //                                 constraints: BoxConstraints(
      //                                   minHeight: 76.w
      //                                 ),
      //                                 child: Column(
      //                                   mainAxisAlignment: MainAxisAlignment.start,
      //                                   crossAxisAlignment: CrossAxisAlignment.start,
      //                                   children: [
      //                                     Text(
      //                                       '${productItem.title}',
      //                                       overflow: TextOverflow.ellipsis,
      //                                       maxLines: 2,
      //                                       style: TextStyle(
      //                                         fontSize: 14.sp,
      //                                         color: textBlackColor
      //                                       ),
      //                                     ),
      //                                     SizedBox(height: 12.w),
      //                                     Text(
      //                                       '${item.createdAt}',
      //                                       style: TextStyle(
      //                                         color: textSubtitleColor,
      //                                         fontSize: 12.sp
      //                                       ),
      //                                     )
      //                                   ],
      //                                 ),
      //                               ))
      //                             ],
      //                           ),
      //                         );
      //                       }).toList(),
      //                     ),
      //                     Container(
      //                       alignment: Alignment.centerRight,
      //                       child: Text.rich(
      //                         TextSpan(
      //                           text: '合计支付：',
      //                           style: TextStyle(
      //                             color: textBlackColor,
      //                             fontSize: 14.sp,
      //                             fontWeight: FontWeight.bold
      //                           ),
      //                           children: [
      //                             TextSpan(
      //                               text: '¥',
      //                               style: TextStyle(
      //                                 color: textBlackColor,
      //                                 fontSize: 12.sp,
      //                                 fontWeight: FontWeight.bold
      //                               )
      //                             ),
      //                             TextSpan(
      //                               text: '${item.totalAmount}',
      //                               style: TextStyle(
      //                                 color: textBlackColor,
      //                                 fontSize: 16.sp,
      //                                 fontWeight: FontWeight.bold
      //                               )
      //                             )
      //                           ]
      //                         )
      //                       ),
      //                     )
      //                   ],
      //                 ),
      //               ),
      //               Container(
      //                 padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
      //                 alignment: Alignment.centerRight,
      //                 child: item.status == 1 ? SeckillOrderBtns(
      //                   status: item.status!, 
      //                   toPayFun: () {
      //                     widget.seckillOrderPageController.goPay(item.payableAmount!, item.orderSn!, item.id!, item.status!);
      //                   }, 
      //                   cleanOrderFun: () {
      //                     widget.seckillOrderPageController.closeSeckillOrder(item.id!);
      //                   },
      //                   takeDeliveryFun: () {},
      //                   confirmReceiptFun: () {}
      //                 ) : Container(
      //                   width: 90.w,
      //                   margin: EdgeInsets.only(left: 12.w),
      //                   child: ButtonUtil(
      //                     text: '查看订单', 
      //                     fontSize: 14.w, 
      //                     borderRadius: 18.w, 
      //                     height: 36.h, 
      //                     onTop: () {
      //                       Get.toNamed(
      //                         '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAGE}${AppRouters.SECKILL_ORDER_DETAIL_PAGE}',
      //                         arguments: {
      //                           'seckillOrderId': item.id!,
      //                           'isSeckillPage': false,
      //                           'title': item.statusStr
      //                         }
      //                       );
      //                     }
      //                   ),
      //                 )
      //               )
      //             ],
      //           ),
      //         ),
      //       ), () {
      //         Get.toNamed(
      //           '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAGE}${AppRouters.SECKILL_ORDER_DETAIL_PAGE}',
      //           arguments: {
      //             'seckillOrderId': item.id!,
      //             'isSeckillPage': false,
      //             'title': item.statusStr
      //           }
      //         );
      //       }),
      //     ),
      //   );
      // }).toList()
    );
  }

  @override
  bool get wantKeepAlive => true;
}